package cn.edu.tju.elm.service;

import cn.edu.tju.elm.model.Business;
import cn.edu.tju.elm.model.Food;
import cn.edu.tju.elm.repository.FoodRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;

/**
 * ClassName: FoodService
 * Package: cn.edu.tju.elm.service
 * Description:
 *
 * @Author Niu Wenhang
 * @Create 2025/9/17 21:39
 * @Version 1.0
 */
@Service
@Transactional
public class FoodService {
    private final FoodRepository foodRepository;

    public FoodService(FoodRepository foodRepository) {
        this.foodRepository = foodRepository;
    }

    public Food addFood(Food food) {
        return foodRepository.save(food);
    }

    public Long getBusinessIdByFoodId(Long foodId) {
        return foodRepository.findBusinessId(foodId);
    }

    public Business getBusinessByFoodId(Long foodId) {
        return foodRepository.findBusiness(foodId);
    }

    public BigDecimal getFoodPriceByFoodId(Long foodId) {
        return foodRepository.findFoodPrice(foodId);
    }

    public Optional<Food> getFoodById(Long id) {
        return foodRepository.findById(id);
    }

    public List<Food> getAllFoods() {
        return foodRepository.findByDeletedFalse();
    }

    public List<Food> getFoodsByBusinessId(Long businessId) {
        return foodRepository.findByBusinessIdAndDeletedFalse(businessId);
    }

    public Food updateFood(Food food) {
        return foodRepository.save(food);
    }

    public void deleteFood(Long id) {
        foodRepository.deleteById(id);
    }

    public List<Food> getFoodsByNameContaining(String name) {
        return foodRepository.findByFoodNameContainingAndDeletedFalse(name);
    }

    public List<Food> getFoodsByPriceRange(BigDecimal minPrice, BigDecimal maxPrice) {
        return foodRepository.findByFoodPriceBetweenAndDeletedFalse(minPrice, maxPrice);
    }

    public List<Food> getOnSaleFoods(Long businessId) {
        if (businessId != null) {
            return foodRepository.findByOnSaleTrueAndBusinessIdAndDeletedFalse(businessId);
        } else {
            return foodRepository.findByOnSaleTrueAndDeletedFalse();
        }
    }

    /**
     * 搜索商品
     * @param name 商品名称（可选）
     * @param minPrice 最低价格（可选）
     * @param maxPrice 最高价格（可选）
     * @param onSale 是否在售（可选）
     * @return 符合条件的商品列表
     */
    public List<Food> searchFoods(String name, BigDecimal minPrice, BigDecimal maxPrice, Boolean onSale) {
        List<Food> foods = foodRepository.findByDeletedFalse();
        
        // 根据名称过滤
        if (name != null && !name.trim().isEmpty()) {
            foods = foods.stream()
                    .filter(food -> food.getFoodName().toLowerCase().contains(name.toLowerCase()))
                    .collect(java.util.stream.Collectors.toList());
        }
        
        // 根据价格范围过滤
        if (minPrice != null) {
            foods = foods.stream()
                    .filter(food -> food.getFoodPrice().compareTo(minPrice) >= 0)
                    .collect(java.util.stream.Collectors.toList());
        }
        
        if (maxPrice != null) {
            foods = foods.stream()
                    .filter(food -> food.getFoodPrice().compareTo(maxPrice) <= 0)
                    .collect(java.util.stream.Collectors.toList());
        }
        
        // 根据在售状态过滤
        if (onSale != null) {
            foods = foods.stream()
                    .filter(food -> onSale.equals(food.getOnSale()))
                    .collect(java.util.stream.Collectors.toList());
        }
        
        return foods;
    }
}
